package com.leyou.upload.web;

import com.leyou.upload.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
@RestController
@RequestMapping("upload")
public class UploadController {

    @Autowired
    private UploadService uploadService;


    //图片的上传
    @PostMapping("image")
    public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file) {
        return ResponseEntity.ok(uploadService.uploadImage(file));
    }
}
*/

@RestController
@RequestMapping
public class UploadController{

    @Autowired
    private UploadService uploadService;

    @PostMapping("image") // 文件上传类型，MultipartFile file。
    // 当文件上传时，SpringMVC会自动将上传的文件封装到MultipartFile对象中，可以使用这个对象来接收文件，并且保存
    public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file){
//        String url = uploadService.uploadImage(file);
//        return ResponseEntity.ok(url);
        // 推荐写法：
        return ResponseEntity.ok(uploadService.uploadImage(file));
    }
}


